package com.airlines.crewassignment.domain;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Duration;



/**
 * @author xxxx
 * @date 2018年3月26日 
 * @description 时间段
 */


 
public class DateTimeRange {
	private Date start;
	private Date end;
	

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public DateTimeRange(DateTime start, DateTime end) {
		this.start = start.toDate();
		this.end = end.toDate();
	}

	public DateTimeRange(Date start, Date end) {
		this.start = start;
		this.end = end;
	}
	/**
	 * 获取分钟间隔
	 * @return
	 */
	public int getDuration(){
		DateTime dateTimeStart = new DateTime(this.start);
		DateTime dateTimeEnd = new DateTime(this.end);
		Duration duration = new Duration(dateTimeStart, dateTimeEnd);
		int durationInt = 0;
		long durationLong = duration.getStandardMinutes();
		if(durationLong <= Integer.MAX_VALUE && durationLong >= Integer.MIN_VALUE){
			durationInt = (int) duration.getStandardMinutes();
		}else if(durationLong >= Integer.MAX_VALUE){
			durationInt =  Integer.MAX_VALUE;
		}else{
			durationInt =  Integer.MIN_VALUE;
		}
		return durationInt;
	}
	
	public String getHHmm(){
		int durationInt = this.getDuration();
		int mm = durationInt % 60;
		int hour = (durationInt - mm)/60;
	    return String.format("%02d", hour)+":"+String.format("%02d", mm);      
	}
	
	/**
	 * 根据开始日期，结束日期，返回该日期范围内的月份区间
	 * @return
	 */
	public DateTimeRange getMonthRange(){
		DateTime dateTimeStart = new DateTime(this.start);
		DateTime dateTimeEnd = new DateTime(this.end);
		DateTime monthStart = dateTimeStart.dayOfMonth().withMinimumValue();
		DateTime monthEnd = dateTimeEnd.dayOfMonth().withMaximumValue();
		return new DateTimeRange(monthStart.toDate(),monthEnd.toDate());
	}
	
	
	/**
	 * 根据开始日期，结束日期，返回该日期范围内的月份区间
	 * @return
	 */
	public DateTimeRange getYearRange(){
		DateTime dateTimeStart = new DateTime(this.start);
		DateTime dateTimeEnd = new DateTime(this.end);
		DateTime yearStart = dateTimeStart.dayOfYear().withMinimumValue();
		DateTime yearEnd = dateTimeEnd.dayOfYear().withMaximumValue();
		return new DateTimeRange(yearStart.toDate(),yearEnd.toDate());
	}
	
	@Override
	public String toString() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return df.format(this.start) +" ~ " +df.format(this.end);
	}
}
